package com.gopay.common.trace.hessian;

import com.gopay.common.trace.LogConst;
import com.gopay.common.trace.def.Span;
import com.gopay.common.trace.def.Tracer;
import com.gopay.common.trace.enums.TraceType;
import com.gopay.common.trace.helper.LogAbstractHelper;
import com.gopay.common.trace.helper.LogRpcServerHelper;
import com.gopay.common.trace.support.LogContext;
import com.gopay.common.trace.util.SamplingUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.support.AopUtils;
import org.springframework.remoting.caucho.HessianServiceExporter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * 重写exportor类， 获取hessian处理时间
 * @author ddh02
 * @LogHessianServiceExporter
 * @2017/4/7 9:54  www.gopay.com.cn Inc.All rights reserved.
 */

public class LogHessianServiceExporter extends HessianServiceExporter {

    //private static final Logger logger = LoggerFactory.getLogger(LogHessianServiceExporter.class);

    /***
     * 自定义 exporter 并保持原有逻辑
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {

        LogAbstractHelper helper = new LogRpcServerHelper();

        helper.beforeT(request, response);

        try {
            // 调用spring自身的实现
            super.handleRequest(request, response); // 这里会执行具体业务
        } catch (ServletException e) {
            helper.exT(e);
            throw  e;
        } finally {
            helper.afterT(request, response);
        }
    }


}
